package com.lswms.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.lswms.common.exception.ErrorCode;
import com.lswms.common.exception.RenException;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.ConvertUtils;
import com.lswms.common.utils.ZqDateUtil;
import com.lswms.dao.YnBankcardDao;
import com.lswms.dto.YnBankcardDTO;
import com.lswms.dto.vo.req.BankcardReq;
import com.lswms.dto.vo.req.UnBankcardReq;
import com.lswms.entity.YnBankcardEntity;
import com.lswms.entity.YnUserEntity;
import com.lswms.service.YnBankcardService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * 绑卡记录表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-04-25
 */
@Service
@Slf4j
public class YnBankcardServiceImpl extends CrudServiceImpl<YnBankcardDao, YnBankcardEntity, YnBankcardDTO> implements YnBankcardService {

    @Override
    public QueryWrapper<YnBankcardEntity> getWrapper(Map<String, Object> params){
    	Long id = (Long)params.get("id");

        QueryWrapper<YnBankcardEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(id != null, "id", id);

        return wrapper;
    }
    /**
     * 绑卡列表
     */
	@Override
	public List<YnBankcardDTO> bankcardList(YnUserEntity ynuser) {
		log.info("绑卡列表，参数：{}",JSON.toJSONString(ynuser));
		QueryWrapper<YnBankcardEntity> wrapper = new QueryWrapper<YnBankcardEntity>();
		wrapper.eq("user_id", ynuser.getId());
		wrapper.eq("unbind",1);
		wrapper.orderByDesc("create_date");
		
		List<YnBankcardEntity> list = baseDao.selectList(wrapper);
		List<YnBankcardDTO> resp = ConvertUtils.sourceToTarget(list, YnBankcardDTO.class);
		log.info("绑卡列表，返回：{}",JSON.toJSONString(resp));
		return resp;
	}
	/**
	 * 绑卡
	 */
	@Override
	public void bankcard(BankcardReq dto, YnUserEntity ynuser) {
		log.info("绑卡，dto：{},ynuser:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
		//查询卡号是否绑过
		QueryWrapper<YnBankcardEntity> wrapper = new QueryWrapper<YnBankcardEntity>();
		wrapper.eq(StringUtils.isNotBlank(dto.getBankcard()), "bankcard", dto.getBankcard());
		YnBankcardEntity bankcard = baseDao.selectOne(wrapper);
		if(bankcard != null){
			throw new RenException(ErrorCode.IS_BIND);
		}
		//没人最多绑定3张银行卡
		QueryWrapper<YnBankcardEntity> wrapper1 = new QueryWrapper<YnBankcardEntity>();
		wrapper1.eq("user_id", ynuser.getId());
		wrapper1.eq("unbind",1);
		List<YnBankcardEntity> list = baseDao.selectList(wrapper1);
		if(list.size() >= 3){
			throw new RenException(ErrorCode.BANKCARD_IS_MORE);
		}
		//绑卡
		bankcard = new YnBankcardEntity();
		bankcard.setRealname(dto.getRealname());
		bankcard.setBankcard(dto.getBankcard());
		bankcard.setBankname(dto.getBankname());
		bankcard.setMobile(dto.getMobile());
		bankcard.setCreateDate(ZqDateUtil.getNow());
		bankcard.setUserId(ynuser.getId());
		baseDao.insert(bankcard);
	}
	/**
	 * 解绑
	 */
	@Override
	public void unbankcard(UnBankcardReq dto, YnUserEntity ynuser) {
		log.info("解绑银行卡dto:{},ynuser:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
		YnBankcardEntity card = baseDao.selectById(dto.getId());
		//判断银行卡是否存在
		if(card == null){
			throw new RenException(ErrorCode.BANKCARD_IS_NOEXIST);
		}
		//判断是否和当前用户对应
		if(ynuser.getId() != card.getUserId()){
			throw new RenException(ErrorCode.BANKCARD_IS_ERROE);
		}
		//解绑
		YnBankcardEntity entity = new YnBankcardEntity();
		entity.setId(card.getId());
		entity.setUnbind(0);
		entity.setUpdateDate(ZqDateUtil.getNow());
		baseDao.updateById(entity);
		
	}


}